//
//  RGShareView.m
//  RedGrass
//
//  Created by wukong on 2018/12/13.
//  Copyright © 2018年 hongcaosp. All rights reserved.
//

#import "RGShareView.h"
#import "RGMainPageShareItem.h"

#define itemWidth 80
#define BaseTag 10000

@interface RGShareView()

@property (strong, nonatomic) UIView *containerView;
@property (strong, nonatomic) UILabel *shareUserLabel;
@property (strong, nonatomic) UILabel *shareOtherAppLabel;
@property (strong, nonatomic) UIView *line;
@property (strong, nonatomic) UIButton *backBtn;
@property (strong, nonatomic) UIScrollView *topScrollView;
@property (strong, nonatomic) UIScrollView *midScrollView;
@property (strong, nonatomic) UIScrollView *bottomScrollView;

@property(strong, nonatomic) NSArray *shareUserList;//分享给其它用户
@property(strong, nonatomic) NSArray *sareOtherAppList; //分享给其它应用
@property(strong, nonatomic) NSArray *localShareList; //应用本身的操作

@end

@implementation RGShareView


-(instancetype) initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        [self initUI];
        [self addMasonry];
        [self initBottomScrollView];
//        [self initMidScrollView];
    }
    return self;
}

-(void)initUI{
    
    self.backgroundColor = [UIColor clearColor];
    [self addSubview:self.containerView];
    [_containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    
    UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10.0f, 10.0f)];
    CAShapeLayer* shape = [[CAShapeLayer alloc] init];
    [shape setPath:rounded.CGPath];
    self.containerView.layer.mask = shape;
    UIBlurEffect *blurEffect =[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    UIVisualEffectView *visualEffectView = [[UIVisualEffectView alloc]initWithEffect:blurEffect];
    visualEffectView.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
    visualEffectView.alpha = 1.0f;
    [self.containerView addSubview:visualEffectView];
//
//    [self.containerView addSubview:self.shareUserLabel];
//    [self.containerView addSubview:self.backBtn];
//    [self.containerView addSubview:self.topScrollView];
//    [self.containerView addSubview:self.shareOtherAppLabel];
//    [self.containerView addSubview:self.midScrollView];
//    [self.containerView addSubview:self.line];
    [self.containerView addSubview:self.bottomScrollView];
}

-(void) addMasonry{
//    [_shareUserLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(self.containerView).mas_offset(10);
//        make.top.mas_equalTo(self.containerView).mas_offset(20);
//    }];
//
//    [_backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.mas_equalTo(self.containerView).mas_offset(-10);
//        make.top.mas_equalTo(self.containerView).mas_offset(10);
//    }];
//
//    [_topScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(self.containerView).mas_offset(0);
//        make.top.mas_equalTo(self.shareUserLabel.mas_bottom).mas_offset(8);
//        make.right.mas_equalTo(self.containerView).mas_offset(0);
//        make.height.mas_equalTo(itemWidth);
//    }];
//
//    [_shareOtherAppLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(self.containerView).mas_offset(10);
//        make.top.mas_equalTo(self.topScrollView.mas_bottom).mas_offset(8);
//    }];
//
//    [_midScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(self.containerView).mas_offset(0);
//        make.top.mas_equalTo(self.shareOtherAppLabel.mas_bottom).mas_offset(8);
//        make.right.mas_equalTo(self.containerView).mas_offset(0);
//        make.height.mas_equalTo(itemWidth - 10);
//    }];
//
//    [_line mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(self.midScrollView.mas_bottom).mas_offset(8);
//        make.left.mas_equalTo(self.containerView).mas_offset(15);
//        make.right.mas_equalTo(self.containerView).mas_offset(-15);
//        make.height.mas_equalTo(0.5);
//    }];
    
    [_bottomScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.containerView).mas_offset(0);
        make.top.mas_equalTo(self.containerView).mas_offset(30);
        make.right.mas_equalTo(self.containerView).mas_offset(0);
        make.height.mas_equalTo(itemWidth);
    }];
}

-(void) initMidScrollView{
     RGMainPageShareItem *item1 = [RGMainPageShareItem initShareItemWithIconImg:@"微信" title:@""];
    [_midScrollView addSubview:item1];
    [item1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(itemWidth, itemWidth));
        make.centerY.mas_equalTo(self.midScrollView).mas_offset(0);
        make.left.mas_equalTo(self.midScrollView).mas_offset(4);
    }];
    _midScrollView.contentSize = CGSizeMake(itemWidth * 1 + 4 * 1, itemWidth - 10);
}

-(void) initBottomScrollView{
    RGMainPageShareItem *item1 = [RGMainPageShareItem initShareItemWithIconImg:@"icon_share_message" title:@"私信"];
    RGMainPageShareItem *item2 = [RGMainPageShareItem initShareItemWithIconImg:@"icon_share_reforword" title:@"转发"];
    RGMainPageShareItem *item3 = [RGMainPageShareItem initShareItemWithIconImg:@"微信" title:@"分享到"];
    RGMainPageShareItem *item4 = [RGMainPageShareItem initShareItemWithIconImg:@"保存本地" title:@"保存"];
    [_bottomScrollView addSubview:item1];
    AA_WEAK_MAKE(self)weakSelf = self;
    [item1 setActionBtnClickBlock:^(NSInteger tag) {
        if([weakSelf.shareViewDelegate respondsToSelector:@selector(shareChosedWithView:chosedIndex:)]){
            [weakSelf.shareViewDelegate shareChosedWithView:weakSelf chosedIndex:kShareMessageTag];
        }
    }];
    [_bottomScrollView addSubview:item2];
    [item2 setActionBtnClickBlock:^(NSInteger tag) {
        if([weakSelf.shareViewDelegate respondsToSelector:@selector(shareChosedWithView:chosedIndex:)]){
            [weakSelf.shareViewDelegate shareChosedWithView:weakSelf chosedIndex:kSharePushToOtherTag];
        }
    }];
    [_bottomScrollView addSubview:item3];
    [item3 setActionBtnClickBlock:^(NSInteger tag) {
        if([weakSelf.shareViewDelegate respondsToSelector:@selector(shareChosedWithView:chosedIndex:)]){
            [weakSelf.shareViewDelegate shareChosedWithView:weakSelf chosedIndex:kShareToOtherTag];
        }
    }];
    [_bottomScrollView addSubview:item4];
    [item4 setActionBtnClickBlock:^(NSInteger tag) {
        if([weakSelf.shareViewDelegate respondsToSelector:@selector(shareChosedWithView:chosedIndex:)]){
            [weakSelf.shareViewDelegate shareChosedWithView:weakSelf chosedIndex:kShareSaveTag];
        }
    }];
    
    [item1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(itemWidth, itemWidth));
        make.centerY.mas_equalTo(self.bottomScrollView).mas_offset(0);
        make.left.mas_equalTo(self.bottomScrollView).mas_offset(4);
    }];
    
    [item2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(itemWidth, itemWidth));
        make.centerY.mas_equalTo(self.bottomScrollView).mas_offset(0);
        make.left.mas_equalTo(self.bottomScrollView).mas_offset(4* 2 + itemWidth);
    }];
    
    [item3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(itemWidth, itemWidth));
        make.centerY.mas_equalTo(self.bottomScrollView).mas_offset(0);
        make.left.mas_equalTo(self.bottomScrollView).mas_offset(4* 3 + itemWidth * 2);
    }];
    
    [item4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(itemWidth, itemWidth));
        make.centerY.mas_equalTo(self.bottomScrollView).mas_offset(0);
        make.left.mas_equalTo(self.bottomScrollView).mas_offset(4* 4 + itemWidth * 3);
    }];
    
    _bottomScrollView.contentSize = CGSizeMake(itemWidth * 4 + 4 * 4, itemWidth);
    
}

#pragma mark - public

-(void) initTopScrollViewWithUserArr:(NSArray *)userArr{
    if(userArr.count == 0){
        return;
    }
    
    [_topScrollView removeAllSubviews];
    
    _shareUserList = userArr;
    for (NSInteger index = 0 ; index < userArr.count; index ++) {
        NSDictionary *dic = userArr[0];
        NSString *url = [dic objectForKey:@"imgUrl"];
        NSString *title = [dic objectForKey:@"title"];
        if(title.length == 0){
            title = @"匿名用户";
        }
        RGMainPageShareItem *item = [RGMainPageShareItem initShareItemWithIconImg:SafeStr(url) title:SafeStr(title)];
        item.tag = BaseTag + index;
        CGFloat offset = 4 * (index + 1) + itemWidth * index;
        [_topScrollView addSubview:item];
        [item mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(itemWidth, itemWidth));
            make.centerY.mas_equalTo(self.topScrollView).mas_offset(0);
            make.left.mas_equalTo(self.topScrollView).mas_offset(offset);
        }];
        
        AA_WEAK_MAKE(self)weakSelf = self;
        [item setActionBtnClickBlock:^(NSInteger tag) {
            if([weakSelf.shareViewDelegate respondsToSelector:@selector(shareChosedWithShareView:shareType:chosedIndex:)]){
                [weakSelf.shareViewDelegate shareChosedWithShareView:weakSelf shareType:RGShareType_user chosedIndex:tag];
            }
        }];
    }
    NSInteger itemCount = userArr.count;
     _topScrollView.contentSize = CGSizeMake(itemCount * (4 + itemWidth), itemWidth);
}

#pragma mark - reponse

-(void)backBtnClick:(UIButton *)button{
    if([self.shareViewDelegate respondsToSelector:@selector(closeViewWithShareView)]){
        [self.shareViewDelegate closeViewWithShareView];
    }
}

#pragma mark - getter
-(UILabel *)shareUserLabel{
    if(!_shareUserLabel){
        _shareUserLabel = [[UILabel alloc]init];
        _shareUserLabel.textColor = [UIColor whiteColor];
        _shareUserLabel.font = FONT(customBoldFontName, 13);
        _shareUserLabel.text = @"私信给";
    }
    return _shareUserLabel;
}

-(UILabel *)shareOtherAppLabel{
    if(!_shareOtherAppLabel){
        _shareOtherAppLabel = [[UILabel alloc]init];
        _shareOtherAppLabel.textColor = [UIColor whiteColor];
        _shareOtherAppLabel.font = FONT(customBoldFontName, 13);
        _shareOtherAppLabel.text = @"分享到";
    }
    return _shareOtherAppLabel;
}

-(UIButton *)backBtn{
    if(!_backBtn){
        _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backBtn setBackgroundImage:IMAGE_NAMED(@"关闭1") forState:UIControlStateNormal];
        [_backBtn addTarget:self action:@selector(backBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backBtn;
}

-(UIView *)line{
    if(!_line){
        _line = [[UIView alloc]init];
        _line.backgroundColor = UICOLOR_WITH_RGBINT(0x9997a3);
    }
    return _line;
}

-(UIScrollView *)topScrollView{
    if(!_topScrollView){
        _topScrollView = [[UIScrollView alloc]init];
        _topScrollView.showsHorizontalScrollIndicator = NO;
    }
    return _topScrollView;
}

-(UIScrollView *)midScrollView{
    if(!_midScrollView){
        _midScrollView = [[UIScrollView alloc]init];
        _midScrollView.showsHorizontalScrollIndicator = NO;
    }
    return _midScrollView;
}

-(UIScrollView *)bottomScrollView{
    if(!_bottomScrollView){
        _bottomScrollView = [[UIScrollView alloc]init];
        _bottomScrollView.showsHorizontalScrollIndicator = NO;
    }
    return _bottomScrollView;
}

-(UIView *)containerView{
    if(!_containerView){
        _containerView = [[UIView alloc]init];
        _containerView.backgroundColor = UICOLOR_WITH_RGBINT_A(0x000000, 0.6);
    }
    return _containerView;
}

@end
